/**
 * 此文件用来配置路由规则的文件
 */

// import router from

// 导入组件
// import Index from '@/views/Index'
// import Login from '@/views/Login'
// import Register from '@/views/Register'
// import Search from '@/views/Search'
// import Test from '@/views/Test'
// import Detail from '@/views/Detail'
// import AddCartSuccess from '@/views/AddCartSuccess'
// import ShopCart from '@/views/ShopCart'
// import Trade from '@/views/Trade'
// import Pay from '@/views/Pay'
// import PaySuccess from '@/views/PaySuccess'
// import Center from '@/views/Center'

// 将组件导入改成函数模式(路由懒加载模式)

const Index = () => import('@/views/Index')
const Login = () => import('@/views/Login')
const Register = () => import('@/views/Register')
const Search = () => import('@/views/Search')
const Detail = () => import('@/views/Detail')
const AddCartSuccess = () => import('@/views/AddCartSuccess')
const ShopCart = () => import('@/views/ShopCart')
const Trade = () => import('@/views/Trade')
const Pay = () => import('@/views/Pay')
const PaySuccess = () => import('@/views/PaySuccess')
const Center = () => import('@/views/Center')
const Test = () => import('@/views/Test')
// 此文件只配置路由规则
const routes = [
  {
    // 跟路由
    path: '/',
    redirect: '/index'
  },
  {
    // 首页
    path: '/index',
    // 也可以再路由 component的值上写成一个函数
    // component: () => ('@/views/Index')
    component: Index
  },
  {
    // 登录
    path: '/login',
    component: Login,
    meta: {
      isHiddenFouter: true
    }
  },
  {
    // 注册
    path: '/register',
    component: Register,
    meta: {
      isHiddenFouter: true
    }
  },
  {
    // 搜索
    path: '/search',
    component: Search,
    // 路由独享守卫
    // 给某个路由单独分配一个守卫
    beforeEnter: (to, from, next) => {
      // console.log(to)
      // console.log(from)
      next()
    }
  },
  {
    // 商品详情
    path: '/detail/:id',
    component: Detail
  },
  {
    // 加入购物车路由规则
    path: '/addcart_success',
    component: AddCartSuccess,
    // 路由独享守卫
    beforeEnter: (to, from, next) => {
      // 判断是否是从详情界面进入的
      if (from.path.startsWith('/detail')) {
        //
        next()
      } else {
        // router.push('/login')
        next('/login')
      }
    }
  },
  {
    // 购物车界面
    path: '/shopcart',
    component: ShopCart
  },
  {
    // 结算路由
    path: '/trade',
    component: Trade
  },
  {
    // 提交订单
    path: '/pay',
    component: Pay,
    beforeEnter: (to, from, next) => {
      if (from.path === '/trade') {
        next()
      } else {
        next('/')
      }
    }
  },
  {
    // 支付成功
    path: '/pay_success',
    component: PaySuccess,
    beforeEnter: (to, from, next) => {
      if (from.path === '/pay') {
        next()
      } else {
        next('/')
      }
    }
  },
  {
    // 我的订单
    path: '/center',
    component: Center
  },
  {
    // 测试
    path: '/test',
    component: Test
  }
]

export default routes
